使用滑块数值来计算数据集并绘制计算出的数值。

huangapple go评论66阅读模式
英文:

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 &lt;- fluidPage(titlePanel(p(&quot;title&quot;, style = &quot;color:#3474A7&quot;),
 sidebarLayout(
                sidebarPanel(
                    sliderInput(&quot;Max&quot;, &quot;Max:&quot;,
                                min = 0, max = 1000,
                                value = 116.8, step=0.1),
                    sliderInput(&quot;Rate&quot;, &quot;Rate:&quot;,
                                min = 0, max = 5,
                                value = 0.12, step=0.01),
                    sliderInput(&quot;Inflection&quot;, &quot;Inflection:&quot;,
                                min = 0, max = 20,
                                value = 11.06, step=0.01),
                    sliderInput(&quot;Area&quot;, &quot;Area:&quot;,
                                min = 0, max = 10000,
                                value = 180, step=20),
                         p(&quot;Made with&quot;, a(&quot;Shiny&quot;,
                        href = &quot;http://shiny.rstudio.com&quot;), &quot;.&quot;),
                  ),
                  mainPanel(
                    
                    # Output: Table summarizing the values entered ----
                    tableOutput(&quot;values&quot;),
                    plotOutput(&quot;plot&quot;)
                    
                  )
                )
)


#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 &lt;- function(input, output){ 
  
  # Reactive expression to create data frame of all input values ----
  
  sliderValues &lt;- reactive({
    data.frame(
      Name = c(&quot;Max&quot;,
               &quot;Inflection&quot;,
               &quot;Rate&quot;,
               &quot;Area&quot;),
      Value = as.character(c(input$Max,
                             input$Inflection,
                             input$Rate,
                             input$Area)),
      stringsAsFactors = FALSE)
  })
  
# Show the values in an HTML table ----
  output$values &lt;- renderTable({
    sliderValues()
  })
    
#reactive expression to let users download the data as a table 
##restC &lt;- reactive({
  #run the code for all time with the selected parameters from the slider
  mylist&lt;-list(c(1:50))
  mydata&lt;-data.frame(lapply(mylist, Growth, A=values$Max, B=values$Rate, C=values$Inflection, R=values$Area),mylist)
  names(mydata)[1]&lt;-&quot;Pop&quot;
  names(mydata)[2]&lt;-&quot;Time&quot;
  
 #output$my_table&lt;-renderDataTable({
   #restC() 
# })
#plot the values in a graph
 output$plot &lt;- 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 &lt;- fluidPage(titlePanel(p(&quot;title&quot;, style = &quot;color:#3474A7&quot;)),
                           sidebarLayout(
                             sidebarPanel(
                               sliderInput(&quot;Max&quot;, &quot;Max:&quot;,
                                           min = 0, max = 1000,
                                           value = 116.8, step=0.1),
                               sliderInput(&quot;Rate&quot;, &quot;Rate:&quot;,
                                           min = 0, max = 5,
                                           value = 0.12, step=0.01),
                               sliderInput(&quot;Inflection&quot;, &quot;Inflection:&quot;,
                                           min = 0, max = 20,
                                           value = 11.06, step=0.01),
                               sliderInput(&quot;Area&quot;, &quot;Area:&quot;,
                                           min = 0, max = 10000,
                                           value = 180, step=20),
                               downloadButton(&quot;download&quot;, &quot;Download data&quot;),
                               p(&quot;Made with&quot;, a(&quot;Shiny&quot;,
                                                href = &quot;http://shiny.rstudio.com&quot;), &quot;.&quot;),
                             ),
                             mainPanel(
                               
                               # Output: Table summarizing the values entered ----
                               tableOutput(&quot;values&quot;),
                               plotOutput(&quot;plot&quot;)
                               
                             )
                           )
)

#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 &lt;- function(input, output){ 
  
  # Reactive expression to create data frame of all input values ----
  
  sliderValues &lt;- reactive({
    data.frame(
      Name = c(&quot;Max&quot;,
               &quot;Inflection&quot;,
               &quot;Rate&quot;,
               &quot;Area&quot;),
      Value = as.character(c(input$Max,
                             input$Inflection,
                             input$Rate,
                             input$Area)),
      stringsAsFactors = FALSE)
  })
  
  # Show the values in an HTML table ----
  output$values &lt;- renderTable({
    sliderValues()
  })
  
  #reactive expression to let users download the data as a table 
  restC &lt;- reactive({
  #run the code for all time with the selected parameters from the slider
  mylist&lt;-list(c(1:50))
  mydata&lt;-data.frame(lapply(mylist, Growth, A=input$Max, B=input$Rate, C=input$Inflection, R=input$Area),mylist)
  names(mydata)[1]&lt;-&quot;Pop&quot;
  names(mydata)[2]&lt;-&quot;Time&quot;
  mydata
  })
  #output$my_table&lt;-renderDataTable({
  #restC() 
  # })
  #plot the values in a graph
  output$plot &lt;- renderPlot({
    ggplot(restC(), aes(Time,Pop)) + geom_line()
  })
  
  output$download &lt;- downloadHandler(
    filename = function() {
      paste(&quot;data-&quot;, Sys.Date(), &quot;.csv&quot;, sep=&quot;&quot;)
    },
    content = function(file) {
      write.csv(restC(), file)
    }
  )
  
}

shinyApp(ui = ui, server = server)

huangapple
  • 本文由 发表于 2023年6月2日 02:52:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76384877.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定