英文:
Rshiny-Use slider values to cacluate a dataset and plot the calculated values
问题
我是你的中文翻译,以下是翻译好的代码部分:
我是一个相对新手的Rshiny用户,正在寻求帮助以了解如何使用滑块值创建图表。用户选择的滑块值显示为表格,并用作计算方程的输入。计算得到的结果值存储在表格中(如果可能的话,我想能够将生成的值下载为csv文件),并用于生成一个简单的图表。以下是我目前的代码:
ui <- fluidPage(titlePanel(p("title", style = "color:#3474A7"),
sidebarLayout(
sidebarPanel(
sliderInput("Max", "Max:",
min = 0, max = 1000,
value = 116.8, step=0.1),
sliderInput("Rate", "Rate:",
min = 0, max = 5,
value = 0.12, step=0.01),
sliderInput("Inflection", "Inflection:",
min = 0, max = 20,
value = 11.06, step=0.01),
sliderInput("Area", "Area:",
min = 0, max = 10000,
value = 180, step=20),
p("Made with", a("Shiny",
href = "http://shiny.rstudio.com"), "."),
),
mainPanel(
# 输出:总结输入值的表格 ----
tableOutput("values"),
plotOutput("plot")
)
)
)
# 使用滑块值来估计任何给定年份的增长,使用以下方程
Growth = function(x, A, B, C, R)
{R *(A *exp(-B * C^x))}
# 创建一个包含所选滑块值的表格输出
server <- function(input, output){
sliderValues <- reactive({
data.frame(
Name = c("Max",
"Inflection",
"Rate",
"Area"),
Value = as.character(c(input$Max,
input$Inflection,
input$Rate,
input$Area)),
stringsAsFactors = FALSE)
})
# 在HTML表格中显示值 ----
output$values <- renderTable({
sliderValues()
})
output$plot <- renderPlot({
ggplot(mydata, aes(Time,Pop)) + geom_line()
})
}
shinyApp(ui = ui, server = server)
请注意,我已经保留了原始代码中的HTML和R代码部分。如果需要进一步的翻译或解释,请告诉我。
英文:
I'm fairly new to Rshiny and looking for some help to understand how to create a plot using slider values as input. The user selected slider values are displayed as a table, and used as inputs to calculate an equation. The resulting calculated values are stored in a table (if possible I'd like to be able to download the generated values as a csv file) and used to generate a simple plot. This is what I have so far:
ui <- fluidPage(titlePanel(p("title", style = "color:#3474A7"),
sidebarLayout(
sidebarPanel(
sliderInput("Max", "Max:",
min = 0, max = 1000,
value = 116.8, step=0.1),
sliderInput("Rate", "Rate:",
min = 0, max = 5,
value = 0.12, step=0.01),
sliderInput("Inflection", "Inflection:",
min = 0, max = 20,
value = 11.06, step=0.01),
sliderInput("Area", "Area:",
min = 0, max = 10000,
value = 180, step=20),
p("Made with", a("Shiny",
href = "http://shiny.rstudio.com"), "."),
),
mainPanel(
# Output: Table summarizing the values entered ----
tableOutput("values"),
plotOutput("plot")
)
)
)
#use the slider values to estimate growth for any given year using the equation
Growth = function(x, A, B, C, R)
{R *(A *exp(-B * C^x))}
#create a Table Ouptut with selected Slider values
server <- function(input, output){
# Reactive expression to create data frame of all input values ----
sliderValues <- reactive({
data.frame(
Name = c("Max",
"Inflection",
"Rate",
"Area"),
Value = as.character(c(input$Max,
input$Inflection,
input$Rate,
input$Area)),
stringsAsFactors = FALSE)
})
# Show the values in an HTML table ----
output$values <- renderTable({
sliderValues()
})
#reactive expression to let users download the data as a table
##restC <- reactive({
#run the code for all time with the selected parameters from the slider
mylist<-list(c(1:50))
mydata<-data.frame(lapply(mylist, Growth, A=values$Max, B=values$Rate, C=values$Inflection, R=values$Area),mylist)
names(mydata)[1]<-"Pop"
names(mydata)[2]<-"Time"
#output$my_table<-renderDataTable({
#restC()
# })
#plot the values in a graph
output$plot <- renderPlot({
ggplot(mydata, aes(Time,Pop)) + geom_line()
})
}
shinyApp(ui = ui, server = server)
答案1
得分: 0
对您的代码进行了一些调整,现在它可以实现您想要的功能:
library(shiny)
library(ggplot2)
ui <- fluidPage(titlePanel(p("标题", style = "color:#3474A7")),
sidebarLayout(
sidebarPanel(
sliderInput("Max", "最大值:",
min = 0, max = 1000,
value = 116.8, step = 0.1),
sliderInput("Rate", "速率:",
min = 0, max = 5,
value = 0.12, step = 0.01),
sliderInput("Inflection", "拐点:",
min = 0, max = 20,
value = 11.06, step = 0.01),
sliderInput("Area", "区域:",
min = 0, max = 10000,
value = 180, step = 20),
downloadButton("download", "下载数据"),
p("使用", a("Shiny",
href = "http://shiny.rstudio.com"), "制作。"),
),
mainPanel(
# 输出:汇总输入值的表格 ----
tableOutput("values"),
plotOutput("plot")
)
)
)
# 使用滑块值估算任意给定年份的增长,使用方程式
Growth = function(x, A, B, C, R)
{R * (A * exp(-B * C^x))}
# 创建一个带有所选滑块值的表格输出
server <- function(input, output){
# 用于创建包含所有输入值的数据框的反应表达式 ----
sliderValues <- reactive({
data.frame(
Name = c("Max",
"Inflection",
"Rate",
"Area"),
Value = as.character(c(input$Max,
input$Inflection,
input$Rate,
input$Area)),
stringsAsFactors = FALSE)
})
# 在HTML表格中显示值 ----
output$values <- renderTable({
sliderValues()
})
# 用于让用户下载数据表的反应表达式
restC <- reactive({
# 使用从滑块选择的参数运行代码的所有时间
mylist <- list(c(1:50))
mydata <- data.frame(lapply(mylist, Growth, A = input$Max, B = input$Rate, C = input$Inflection, R = input$Area), mylist)
names(mydata)[1] <- "Pop"
names(mydata)[2] <- "Time"
mydata
})
# 绘制图表中的值
output$plot <- renderPlot({
ggplot(restC(), aes(Time, Pop)) + geom_line()
})
output$download <- downloadHandler(
filename = function() {
paste("data-", Sys.Date(), ".csv", sep = "")
},
content = function(file) {
write.csv(restC(), file)
}
)
}
shinyApp(ui = ui, server = server)
希望这有所帮助。如果您需要进一步的解释或帮助,请告诉我。
英文:
Made some tweakings in your code; now it does what you want:
library(shiny)
library(ggplot2)
ui <- fluidPage(titlePanel(p("title", style = "color:#3474A7")),
sidebarLayout(
sidebarPanel(
sliderInput("Max", "Max:",
min = 0, max = 1000,
value = 116.8, step=0.1),
sliderInput("Rate", "Rate:",
min = 0, max = 5,
value = 0.12, step=0.01),
sliderInput("Inflection", "Inflection:",
min = 0, max = 20,
value = 11.06, step=0.01),
sliderInput("Area", "Area:",
min = 0, max = 10000,
value = 180, step=20),
downloadButton("download", "Download data"),
p("Made with", a("Shiny",
href = "http://shiny.rstudio.com"), "."),
),
mainPanel(
# Output: Table summarizing the values entered ----
tableOutput("values"),
plotOutput("plot")
)
)
)
#use the slider values to estimate growth for any given year using the equation
Growth = function(x, A, B, C, R)
{R *(A *exp(-B * C^x))}
#create a Table Ouptut with selected Slider values
server <- function(input, output){
# Reactive expression to create data frame of all input values ----
sliderValues <- reactive({
data.frame(
Name = c("Max",
"Inflection",
"Rate",
"Area"),
Value = as.character(c(input$Max,
input$Inflection,
input$Rate,
input$Area)),
stringsAsFactors = FALSE)
})
# Show the values in an HTML table ----
output$values <- renderTable({
sliderValues()
})
#reactive expression to let users download the data as a table
restC <- reactive({
#run the code for all time with the selected parameters from the slider
mylist<-list(c(1:50))
mydata<-data.frame(lapply(mylist, Growth, A=input$Max, B=input$Rate, C=input$Inflection, R=input$Area),mylist)
names(mydata)[1]<-"Pop"
names(mydata)[2]<-"Time"
mydata
})
#output$my_table<-renderDataTable({
#restC()
# })
#plot the values in a graph
output$plot <- renderPlot({
ggplot(restC(), aes(Time,Pop)) + geom_line()
})
output$download <- downloadHandler(
filename = function() {
paste("data-", Sys.Date(), ".csv", sep="")
},
content = function(file) {
write.csv(restC(), file)
}
)
}
shinyApp(ui = ui, server = server)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论