gpt4 book ai didi

r - 如何构建一个长的 selectInput 列表,然后使用它来更改 facet_wrap 中的标签

转载 作者:行者123 更新时间:2023-12-04 20:04:45 28 4
gpt4 key购买 nike

这个问题引用了This SO question

上面的问题是关于更改 facet_wrap 的标签,答案是完美的 --> 将修改后的标签添加为数据集的新列。

现在,我面临的问题是——

用户选择多个变量 selectInput("select", h4("Variables:"), choices=var.both1(), selected=var.both1()[1], multiple=T, width="100%")
(例如:让我们考虑 input$select 长度可以是 6)
现在 input$select包含六个变量,我想检查每个变量并为其分配单位,我可以使用以下 react 组件部分实现这一点

variableunit <- reactive ({
if(input$select == "TEPC") {"degC"}
else if(input$select == "AT"){"µmol/kg"}
else if(input$select == "DIC" | input$select == "DIN" | input$select == "PIC" | input$select == "POC" | input$select == "PON" | input$select == "POP" | input$select == "DOC" | input$select == "DON" | input$select == "DOP" | input$select == "TEP"){c("µmol/L")}
else if(input$select == "Chla"){"µg/L"}
else ("Meters")
})
variableunit这里得到一个单一的值,即使用户输入了 6 个变量, variableunit只能给我一个值。

我怎么能在 variableunit 里面有一个包含 6 个值的列表这样我就可以在下面的 ggplot facet_wrap 中使用它

代码
    server <- function(input, output) {

a <- reactive({
fileinput1 <- input$file1
if (is.null(fileinput1))
return(NULL)
read.table(fileinput1$datapath, header = TRUE, col.names = c("Ei","Mi","hours","Nphy","Cphy","CHLphy","Nhet","Chet","Ndet","Cdet","DON","DOC","DIN","DIC","AT","dCCHO","TEPC","Ncocco","Ccocco","CHLcocco","PICcocco","par","Temp","Sal","co2atm","u10","dicfl","co2ppm","co2mol","pH"))
#read.table(fileinput1$datapath, header = TRUE, col.names = c("Experiment","Mesocosm","Hour","Nphy","Cphy","CHLphy","Nhet","Chet","Ndet","Cdet","DON","DOC","DIN","DIC","AT","dCCHO","TEPC","Ncocco","Ccocco","CHLcocco","PICcocco","PAR","Temperature","Salinity","CO2atm","u10","DICflux","CO2ppm","CO2mol","pH"))
#a$Chla <- a$CHLphy + a$CHLcocco #Add new columns as per observation data
#a$PON <- a$Nphy + a$Nhet + a$Ndet + a$Ncocco
})

output$showMapPlot <- renderUI({
{ list(plotOutput("plot",height="100%"), br()) }
})



output$select <- renderUI({
if(!is.null(a())){selectInput("select", h4("Variables:"), choices=names(a()), selected=NULL, multiple=T, width="100%")}
})


variableunit <- reactive ({
if(input$select == "TEPC") {"degC"}
else if(input$select == "AT"){"µmol/kg"}
else if(input$select == "DIC" | input$select == "DIN" | input$select == "PIC" | input$select == "POC" | input$select == "PON" | input$select == "POP" | input$select == "DOC" | input$select == "DON" | input$select == "DOP" | input$select == "TEP"){c("µmol/L")}
else if(input$select == "Chla"){"µg/L"}
else ("Meters")
})


plot_4 <- function(var1 = input$select[1], var2 = input$select[2], var3 = input$select[3], var4 = input$select[4], var5 = input$select[5], var6 = input$select[6]) {
myvars <- c(var1,var2,var3,var4,var5,var6)
withProgress(message = 'Processing please wait...', value = 0, {
gg4 <- aggregate(cbind(get(var1),get(var2),get(var3),get(var4),get(var5),get(var6))~Mi+hours,a(), FUN=mean)
names(gg4)[3] <- var1
names(gg4)[4] <- var2
names(gg4)[5] <- var3
names(gg4)[6] <- var4
names(gg4)[7] <- var5
names(gg4)[8] <- var6
dd <- melt(gg4,id.vars=c("Mi","hours"), measure.vars=myvars)
dd$label <- paste(as.character(dd$variable), "(", (variableunit()), ")", sep="")
print(ggplot(dd,aes(x=hours, y=value)) +
geom_point(aes(color=factor(Mi)), size = 3, position = position_jitter(width = 0.1)) +
geom_smooth(stat= "smooth" , alpha = I(0.01), method="loess", color = "blue") +
facet_wrap(~label, nrow=3, ncol=2,scales = "free_y") + scale_color_discrete("Mesocosm") )
})
}

output$plot <- renderPlot({
if(length(input$select) == 6){
plot_4()
}
},
height=700, width=1100
)
}

ui <- shinyUI(fluidPage(
fluidRow(column(3,
uiOutput("showMapPlot"),
wellPanel(
h4("Data Upload"),
fileInput('file1', h5('Choose Your Model Data'), accept=c('text/csv','text/comma-separated-values,text/plain','.OUT'))),
wellPanel(h4("Variable selection"),uiOutput("select"))

),
column(9,
tabsetPanel(
tabPanel("Conditional Plots",plotOutput("plot",height="auto"),value="barplots"),
id="tsp"))
)
))

shinyApp(ui = ui, server = server)

文件上传
Download here

只需复制粘贴代码并执行它。

现在问题
是第一个变量单元在所有其他图中重复。我知道这是我用来获取变量单位的 react 组件的问题。

问题
现在是,如何做到这一点?

我被困在这里很长时间了,真的希望有人知道解决方法。谢谢。

最佳答案

问:“如何在 variableunit 中拥有 6 个值的列表,以便我可以在下面的 ggplot facet_wrap 中使用它”

答:您可以在 react 函数中拥有 6 个值的列表。使用for循环遍历input$select .并将相应的单元保存到列表中的相同索引中。

server <- function(input, output) {

variableunit <- reactive({
test <- c("TEPC", "Chla", "DIN", "PIC", "AI", "PON")
values <- list()
for(i in 1:length(test)) {

if(test[[i]] == "TEPC") {
values[[i]] <-"degC"
}else if(test[[i]] == "AT"){
values[[i]] <-"µmol/kg"
}else if(test[[i]] == "DIC" | test[[i]] == "DIN" | test[[i]] == "PIC" | test[[i]] == "POC" | test[[i]] == "PON" | test[[i]] == "POP" | test[[i]] == "DOC" | test[[i]] == "DON" | test[[i]] == "DOP" | test[[i]] == "TEP"){
values[[i]] <-"µmol/L"
}else if(test[[i]] == "Chla"){
values[[i]] <-"µg/L"
}else{
values[[i]] <-"Meters"
}
}

return(paste(as.character(test), "(",values,")", sep=""))
})

output$text <- renderText({
variableunit()
print(paste(variableunit()))
})
}

ui <- shinyUI(fluidPage(
sidebarLayout(
sidebarPanel(

),
mainPanel(textOutput("text"))
)
))

shinyApp(ui = ui, server = server)

此示例呈现文本: TEPC(degC) Chla(µg/L) DIN(µmol/L) PIC(µmol/L) AI(Meters) PON(µmol/L)

关于r - 如何构建一个长的 selectInput 列表,然后使用它来更改 facet_wrap 中的标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27051469/

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