小编agf*_*997的帖子

反应与观察与观察事件的优点

我正在阅读有关闪亮反应式编程的所有内容.我有点困惑.以下所有工作但首选方法是什么?为什么?显然,下面的示例很简单,但是在使用任何方法创建更大的应用程序时,我会遇到麻烦吗?

我一直倾向于倾向于服务器代码#1中的风格.原因是,我能够打破if语句.对我来说,这似乎更具可读性.同样,下面的简单示例并不是非常复杂,但您可以很容易地想象服务器代码2和服务器代码3如何通过大量嵌套的if/if else语句变得非常混乱.

UI代码

library(shiny)

ui <- fluidPage(
  selectInput(inputId = 'choice',
              label = 'Choice',
              choice = c('Hello','Goodbye'),
              selected = c('Hello')
  ),

  textOutput('result')

)
Run Code Online (Sandbox Code Playgroud)

服务器代码1

server <- function(input,output,session)({

  text <- reactiveValues()

  observe({
    if (input$choice == 'Hello') {
      text$result <- 'Hi there'
      }
    })

  observe({
    if (input$choice == 'Goodbye') {
      text$result <- 'See you later'
      }
    })

  output$result <- renderText({
    text$result
  })

})

shinyApp(ui = ui, server = server)
Run Code Online (Sandbox Code Playgroud)

服务器代码2

server <- function(input,output,session)({

  getStatus <- reactive({

    if (input$choice == 'Hello') {
      'Hi …
Run Code Online (Sandbox Code Playgroud)

r shiny

27
推荐指数
2
解决办法
6052
查看次数

Pandas Dataframe to Code

如果我有一个现有的pandas数据帧,有没有办法生成python代码,当在另一个python脚本中执行时,将重现该数据帧.

例如

In[1]: df

Out[1]:
   income   user
0   40000    Bob
1   50000   Jane
2   42000  Alice

In[2]: someFunToWriteDfCode(df)

Out[2]: 
df = pd.DataFrame({'user': ['Bob', 'Jane', 'Alice'], 
    ...:                    'income': [40000, 50000, 42000]})
Run Code Online (Sandbox Code Playgroud)

python pandas

16
推荐指数
1
解决办法
1682
查看次数

自然地排序Pandas DataFrame

我有一个带有索引的pandas DataFrame,我想自然排序.Natsort似乎不起作用.在构建DataFrame之前对索引进行排序似乎没有帮助,因为我对DataFrame的操作似乎搞乱了进程中的排序.关于如何自然地采用指数的任何想法?

from natsort import natsorted
import pandas as pd

# An unsorted list of strings
a = ['0hr', '128hr', '72hr', '48hr', '96hr']
# Sorted incorrectly
b = sorted(a)
# Naturally Sorted 
c = natsorted(a)

# Use a as the index for a DataFrame
df = pd.DataFrame(index=a)
# Sorted Incorrectly
df2 = df.sort()
# Natsort doesn't seem to work
df3 = natsorted(df)

print(a)
print(b)
print(c)
print(df.index)
print(df2.index)
print(df3.index)
Run Code Online (Sandbox Code Playgroud)

python sorting natsort python-2.7 pandas

15
推荐指数
3
解决办法
4428
查看次数

在pandas中合并多索引数据帧

我有2个类似的数据框,结构如下:

ind = pd.MultiIndex.from_product([['Day 1','Day 2'],['D1','D2'],['Mean','StDev','StErr']], names = ['interval','device','stats'])
df =  pd.DataFrame({'col1':[1,2,3,4,5,6,7,8,9,10,11,12]}, index = ind)
print(df)

                       col1
interval device stats      
Day 1    D1     Mean      1
                StDev     2
                StErr     3
         D2     Mean      4
                StDev     5
                StErr     6
Day 2    D1     Mean      7
                StDev     8
                StErr     9
         D2     Mean     10
                StDev    11
                StErr    12

ind2 = pd.MultiIndex.from_product([['Day 1','Day 2'],['D1','D2'],['Ratio']], names = ['interval','device','stats'])
df2 =  pd.DataFrame({'col1':[100,200,300,400]}, index = ind2)
print(df2)

                       col1
interval device stats      
Day 1    D1     Ratio   100
         D2     Ratio   200
Day …
Run Code Online (Sandbox Code Playgroud)

python python-3.x pandas

6
推荐指数
1
解决办法
8298
查看次数

对python列表进行排序,使字母在数字之前出现

我是python的新手,我正在寻找一种方法来排序在数字之前放置单词的列表.

我知道您可以使用sort来执行以下操作:

a = ['c', 'b', 'd', 'a']
a.sort()
print(a)
['a', 'b', 'c', 'd']

b = [4, 2, 1, 3]
b.sort()
print(b)
[1, 2, 3, 4]

c = ['c', 'b', 'd', 'a', 4, 2, 1, 3]
c.sort()
print(c)
[1, 2, 3, 4, 'a', 'b', 'c', 'd']
Run Code Online (Sandbox Code Playgroud)

但是我想要c生产:

['a', 'b', 'c', 'd', 1, 2, 3, 4]
Run Code Online (Sandbox Code Playgroud)

提前致谢

python sorting

5
推荐指数
2
解决办法
6689
查看次数

阴影下的阴影区域

我试图在R的曲线下遮挡一个区域.我不能完全正确,我不知道为什么.曲线由.定义

# Define the Mean and Stdev
mean=1152
sd=84

# Create x and y to be plotted
# x is a sequence of numbers shifted to the mean with the width of sd.  
# The sequence x includes enough values to show +/-3.5 standard deviations in the data set.
# y is a normal distribution for x
x <- seq(-3.5,3.5,length=100)*sd + mean
y <- dnorm(x,mean,sd)
Run Code Online (Sandbox Code Playgroud)

情节是

# Plot x vs. y as a line graph
plot(x, y, type="l")
Run Code Online (Sandbox Code Playgroud)

我用来尝试在x> = …

r

5
推荐指数
1
解决办法
1万
查看次数

Argparse 的 GUI

我在 python 脚本中使用一个简单的 argparse 函数:

def get_args():
    """Get CLI arguments and options"""
    parser = argparse.ArgumentParser(description='AngioTool File Analyzer',
                                     formatter_class=argparse.ArgumentDefaultsHelpFormatter)

    parser.add_argument('rootPath',
                        help="path to files for the experiment",
                        action=FullPaths, type=is_dir)

    parser.add_argument('-c', help='string to specify the control device to which all devices should be ratioed', default='D1')
    parser.add_argument('-p', help="list of fields to plot",
                        default=['Total Vessels Length', 'Total Number of End Points', 'Total Number of Junctions'])
    parser.add_argument('-i', help='string to specify first interval', default='min')
    parser.add_argument('-t', help='comma serperated list with chart titles to be placed on charts', default="Chart …
Run Code Online (Sandbox Code Playgroud)

python pyqt argparse python-3.x

3
推荐指数
1
解决办法
4817
查看次数

创建一个“蛇”计数器

我只是想弄清楚逻辑并使用 Python 来帮助我做到这一点。最终,我需要使用 ImageJ 宏语言来解决这个问题。

我不知道我是否使用了正确的术语,但我想创建一个“蛇”计数器。

x = 1
number = 12
maxVal = 3
minVal = 1

for i in xrange(number):
    %do something
    x = incrementSnakeCounter(x, maxVal, minVal)
    print("i = ", i)
    print("x = ", x)
Run Code Online (Sandbox Code Playgroud)

“蛇”部分使计数器仅上升到maxVal,在下一次迭代中重复该数字,向下计数到minVal,在下一次迭代中重复该值,并重复该过程。

例如,在上面

我希望发生以下情况:

i = 0
x = 1

i = 1
x = 2

i = 2
x = 3

i = 3
x = 3

i = 4
x = 2

i = 5
x = 1 …
Run Code Online (Sandbox Code Playgroud)

python for-loop python-3.x

1
推荐指数
1
解决办法
174
查看次数

标签 统计

python ×6

pandas ×3

python-3.x ×3

r ×2

sorting ×2

argparse ×1

for-loop ×1

natsort ×1

pyqt ×1

python-2.7 ×1

shiny ×1