gpt4 book ai didi

从选择列表到 react 图的 R Shiny 传递变量

转载 作者:行者123 更新时间:2023-12-04 02:07:41 26 4
gpt4 key购买 nike

我第一次尝试使用 Shiny 的包装,太棒了。但是,像往常一样,我面临一些问题。按照 Shiny 的教程和谷歌搜索,我设法显示了不同气象站的两个不同图,但都显示了相同的变量。

enter image description here

我想添加另一个输入列表,以便可以选择要绘制哪个 var。尝试运行脚本时,它似乎没有任何错误地运行,但没有出现绘图,只有选择菜单。
enter image description here

可能在将变量传递给 server.R 时存在错误,因此输出对象图未正确构建,只是猜测。试图以一般方式工作,根据输入变量创建函数,但我遗漏了一些东西,可能是关于 react 性,也许是正确传递变量,......

这些是 ui.R 的代码

    library("shiny")
shinyUI(pageWithSidebar(

headerPanel('Comparación de zonas - Temperatura'),

sidebarPanel(
selectInput("panel1", "Zona:",
list("Zona 1" = "1",
"Zona 2" = "2",
"Zona 3" = "3",
"Zona 4" = "4")),
selectInput("panel2", "Zona:",
list("Zona 1" = "1",
"Zona 2" = "2",
"Zona 3" = "3",
"Zona 4" = "4")),
selectInput("var", "Variable:",
list("tempc" = "tempc",
"relhum" = "relhum")),
helpText('Al seleccionar la zona se crearán automáticamente
el gráfico de evolución temporal.')
),

mainPanel(
conditionalPanel(condition = "inputId == 'panel1'",plotOutput('myplot')
),
conditionalPanel(condition = "inputId == 'panel2'",plotOutput("myplot")
)
)
))

和服务器.R
    library(shiny)
library(plyr)
library(ggplot2)

shinyServer(function(input, output) {

formulaText <- reactive(function() {
paste("Gràfica de ggplot: Zona ", input$zona1)
})

# Return the formula text for printing as a caption
output$caption <- reactiveText(function() {
formulaText()
})

# datasets
datos=read.table("data.dat",header=T)
data=as.data.frame(datos)
data=within(data, datetime <- as.POSIXct(paste(date, time),format = "%Y%m%d %H%M%S"))

rams <- reactive({
subset(data,data$stat_id %in% places$stat_id[places$Zona == input$panel1])
})


plot <- function(var) {
p <- ggplot(rams(),aes(x=datetime, y=var, colour=as.character(stat_id))) +
geom_line()
}

plot=p(input$var)

if(input$var == "tempc") {
plot <- plot + ylab("Temperatura (ºC)") + xlab(" ") +
ggtitle(gtitol) + theme(legend.title=element_blank()) + theme(legend.position="bottom") +
scale_y_continuous(limits = c(-20,ylim),breaks=c(seq(-20,ylim,by=2))) }

if (input$var == "relhum") {
plot <- plot +
ylab("Humedad relativa (%)") + xlab(" ") +
ggtitle(gtitol) + theme(legend.title=element_blank()) + theme(legend.position="bottom") +
scale_y_continuous(limits = c(0,100),breaks=c(seq(0,100,by=5))) }

output$myplot <- reactivePlot(function() {
print(plot)
})

})

预先感谢您的帮助和建议

最佳答案

最后我成功了。问题在于变量从 ui.R 输入传递到 server.R 的方式以及 ggplot 如何处理此类输入。我不得不使用 aes_string 来管理变量名称。现在,两个第一个选择列表允许选择上/下图中使用的数据框,而第三个选择要绘制的变量,通过创建不同的 ggplot 命令。

尽管脚本现在正在运行,但仍有一些问题需要优化代码,以便它可以以更通用的方式工作。我通过对较大的数据框进行子集化来构建两个不同的数据框,也许只有一个和子集会更好。我定义了两个绘图输出(myplot1 和 myplot2),因为我找不到如何仅管理两个条件面板的一个。仍然有工作,但作为 Shiny 的新手,我很高兴它正在运行。

在任何情况下,我都会附上对我有用的代码,并希望它会帮助某人:

用户界面

library("shiny")
shinyUI(pageWithSidebar(

headerPanel('Comparación de zonas - Temperatura'),

sidebarPanel(
selectInput("panel1", "Zona:",
list("Zona 1" = "1",
"Zona 2" = "2",
"Zona 3" = "3")),
selectInput("panel2", "Zona:",
list("Zona 1" = "1",
"Zona 2" = "2",
"Zona 3" = "3")),
selectInput("var", "Variable:",
list("tempc" = "tempc",
"relhum" = "relhum")),
helpText('Al seleccionar la zona se crearán automáticamente
el gráfico de evolución temporal.')
),

mainPanel(
conditionalPanel(condition = "inputId == 'panel1'",plotOutput(outputId='myplot1')),
conditionalPanel(condition = "inputId == 'panel2'",plotOutput(outputId='myplot2'))
)
))

服务器
library(shiny)
library(plyr)
library(ggplot2)

shinyServer(function(input, output) {

datos=read.table("data.dat",header=T)
pobles=read.table("pobles-zona.dat",header=T)

data=as.data.frame(datos)
places=as.data.frame(pobles)

data$time[data$time == "0"] = "000000"
data$time[data$time == "10000"] = "010000"
data$time[data$time == "20000"] = "020000"
data$time[data$time == "30000"] = "030000"
data$time[data$time == "40000"] = "040000"
data$time[data$time == "50000"] = "050000"
data$time[data$time == "60000"] = "060000"
data$time[data$time == "70000"] = "070000"
data$time[data$time == "80000"] = "080000"
data$time[data$time == "90000"] = "090000"

data=within(data, datetime <- as.POSIXct(paste(date, time),format = "%Y%m%d %H%M%S"))

formulaText <- reactive(function() {
paste("Gràfica de ggplot: Zona ", input$panel1, input$panel2, input$var)
})

# Return the formula text for printing as a caption
output$caption <- reactiveText(function() {
formulaText()
})

rams1 <- reactive({
subset(data,data$stat_id %in% places$stat_id[places$Zona == input$panel1])
})
rams2 <- reactive({
subset(data,data$stat_id %in% places$stat_id[places$Zona == input$panel2])
})

p <- function(data){
p=ggplot(data(),aes_string(x="datetime", y=input$var,colour="as.character(stat_id)")) +
geom_line()
}

output$myplot1 <- reactivePlot(function() {

gtitol=paste("Zona ",input$panel1)
yx=round(max(rams1()$tempc)+2)
yn=round(min(rams1()$tempc)-2)

plot=p(rams1)

if ( input$var == "tempc" ) {
plot=plot + ylab("Temperatura (ºC)") + xlab(" ") +
ggtitle(gtitol) + theme(legend.title=element_blank()) + theme(legend.position="bottom") +
scale_y_continuous(limits = c(yn,yx),breaks=c(seq(yn,yx,by=2)))
}

if ( input$var == "relhum" ){
plot=plot + ylab("Humedad relativa (%)") + xlab(" ") +
ggtitle(gtitol) + theme(legend.title=element_blank()) + theme(legend.position="bottom") +
scale_y_continuous(limits = c(0,100),breaks=c(seq(0,100,by=5)))
}
print(plot)
})

output$myplot2 <- reactivePlot(function() {

gtitol=paste("Zona ",input$panel2)
yx=round(max(rams2()$tempc)+2)
yn=round(min(rams2()$tempc)-2)

plot=p(rams2)

if ( input$var == "tempc" ) {
ylim=max(rams2()$tempc)+2
plot=plot + ylab("Temperatura (ºC)") + xlab(" ") +
ggtitle(gtitol) + theme(legend.title=element_blank()) + theme(legend.position="bottom") +
scale_y_continuous(limits = c(yn,yx),breaks=c(seq(yn,yx,by=2)))
}
if ( input$var == "relhum" ) {
ylim=100
plot=plot + ylab("Humedad relativa (%)") + xlab(" ") +
ggtitle(gtitol) + theme(legend.title=element_blank()) + theme(legend.position="bottom") +
scale_y_continuous(limits = c(0,100),breaks=c(seq(0,100,by=5)))
}
print(plot)
})
})

关于从选择列表到 react 图的 R Shiny 传递变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20118158/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com